{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The environment is Python 3.8.3 and gemsin 3.8.3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gensim.downloader as api\n",
    "import numpy as np\n",
    "\n",
    "model_glove_twitter = api.load(\"glove-twitter-200\")\n",
    "# other word embedding models would also work"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### For this behavior tree:\n",
    "                   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"bt-example.png\" alt=\"Alternative text\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Stage 1: word-embedding stage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get embeddings for each leaf node\n",
    "t1 = (model_glove_twitter[\"wrap\"]+model_glove_twitter[\"film\"])/2\n",
    "t2 = (model_glove_twitter[\"pick\"]+model_glove_twitter[\"package\"])/2\n",
    "t3 = (model_glove_twitter[\"place\"]+model_glove_twitter[\"board\"])/2\n",
    "t4 = (model_glove_twitter[\"attach\"]+model_glove_twitter[\"label\"])/2\n",
    "t5 = (model_glove_twitter[\"stack\"]+model_glove_twitter[\"box\"])/2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Stage 2: Node-aggregation stage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# decay factor\n",
    "decay1=np.exp(-0.2*1)\n",
    "decay2=np.exp(-0.2*2)\n",
    "\n",
    "# aggregate the 1st sequence node on layer 2.\n",
    "p1 = (t1+t2*decay1)/2\n",
    "\n",
    "# aggregate the 2nd sequence node on layer 2.\n",
    "p2 = (t3+t4*decay1+t5*decay2)/3\n",
    "\n",
    "# aggregate the root node on layer 1.\n",
    "r1 = (p1+p2*decay1)/2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The r1 will be the embedding of this behavior tree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1.70129463e-01  3.84786502e-02 -1.37636811e-01 -2.37786174e-02\n",
      "  1.86602980e-01  1.04621220e-02  4.03785169e-01 -6.00572675e-05\n",
      "  1.14297926e-01 -5.82272671e-02 -1.31142512e-03  1.92908287e-01\n",
      " -4.65457976e-01  1.89679980e-01  8.60691667e-02 -9.72509012e-03\n",
      " -6.14545122e-03  1.27467275e-01 -2.12400571e-01  2.00934038e-02\n",
      " -1.95538178e-01 -6.20523393e-02 -9.46039557e-02 -3.01047154e-02\n",
      "  1.45018086e-01  6.54836178e-01  9.16860104e-02  1.35474384e-01\n",
      "  3.33437398e-02 -1.86230958e-01  6.50062710e-02 -1.21755779e-01\n",
      " -1.01485342e-01 -2.01378271e-01 -1.52291805e-02 -2.17021808e-01\n",
      "  6.19366206e-02 -9.59758908e-02  2.27433562e-01  1.50729150e-01\n",
      "  3.55079353e-01 -1.77169830e-01 -2.99393497e-02  5.13796881e-02\n",
      "  1.04341418e-01 -6.16368502e-02 -2.67141387e-02 -8.81951526e-02\n",
      "  1.44914210e-01 -4.01286110e-02 -4.01190817e-02  2.96661500e-02\n",
      " -1.47962213e-01  1.22869909e-01 -6.46894723e-02 -1.43791199e-01\n",
      "  2.17718147e-02 -1.15997549e-02 -1.89426854e-01 -1.98803246e-01\n",
      "  2.45586634e-01  1.22498386e-02  1.50156870e-01  1.18779987e-01\n",
      " -2.03295320e-01 -2.43603196e-02  1.01854876e-02 -2.10848190e-02\n",
      "  1.62479818e-01 -9.64255184e-02 -1.32025123e-01 -2.00006768e-01\n",
      " -9.86716002e-02  1.62879243e-01  1.17709585e-01  1.29138201e-01\n",
      "  1.11951828e-01  6.72804378e-03  1.99110672e-01 -1.05655836e-02\n",
      "  5.43489039e-01  6.70258403e-02  1.08400092e-01 -3.99851240e-02\n",
      " -3.73200476e-02 -1.04411012e-02  1.91079259e-01 -3.97663414e-02\n",
      " -1.00797527e-02  1.18412241e-01  6.66722581e-02  2.33472288e-01\n",
      "  3.01911272e-02 -2.22409412e-01  3.98646817e-02  1.36440590e-01\n",
      "  1.64197117e-01 -2.09894583e-01 -2.04165563e-01 -7.40506575e-02\n",
      "  1.80935383e-01  6.84600994e-02 -1.85493827e-02 -3.86939049e-02\n",
      " -1.39459699e-01 -6.82759583e-02  5.68004474e-02 -4.88967374e-02\n",
      "  3.06696426e-02 -1.00421451e-01 -8.06311741e-02 -5.21194413e-02\n",
      "  1.58178866e-01 -1.65403068e-01  2.34596193e-01  4.59192321e-02\n",
      "  1.71664953e-01 -7.60377198e-03 -3.31592187e-02  1.11255255e-02\n",
      " -1.19878374e-01  1.81799337e-01  2.65297648e-02  8.28605331e-03\n",
      " -7.64625520e-03 -2.01726228e-01 -4.40434739e-02  1.52679354e-01\n",
      " -1.73470914e-01  1.11815348e-01 -2.39726886e-01 -1.42903998e-01\n",
      " -1.15638763e-01 -1.37385651e-02 -8.79442766e-02 -1.08160824e-02\n",
      "  1.87030397e-02  2.34398156e-01 -4.15884927e-02  2.39221901e-02\n",
      " -2.56619692e-01 -2.80076396e-02  4.82004844e-02 -7.98878893e-02\n",
      " -2.66199149e-02  6.76829666e-02  1.56906977e-01 -1.82946362e-02\n",
      "  2.74398383e-02  8.83846879e-02  2.75434911e-01 -1.10528313e-01\n",
      " -2.55538225e+00  1.37740880e-01  2.11111024e-01  5.74742816e-02\n",
      " -9.61668342e-02 -3.06719951e-02 -7.16401786e-02 -2.93867290e-02\n",
      " -9.59345028e-02  9.34467763e-02  2.13069797e-01  8.82749408e-02\n",
      "  1.13988295e-01  1.51892945e-01  2.73453258e-03 -6.47561029e-02\n",
      " -7.68570006e-02 -7.30171502e-02  3.01412642e-01  2.39161819e-01\n",
      " -1.18909232e-01  1.14568450e-01 -1.91911608e-02 -1.94478855e-02\n",
      "  4.67894897e-02 -5.88020682e-03  1.16331682e-01 -1.01096369e-02\n",
      " -1.43098295e-01 -1.87527090e-01 -1.73309565e-01  1.06966533e-01\n",
      " -5.62701225e-02  1.57764941e-01 -4.51314002e-02 -8.84718895e-02\n",
      "  8.42326880e-02 -1.66532733e-02  1.59641266e-01 -6.48160875e-02\n",
      " -6.73927590e-02  9.07001421e-02  1.57803982e-01 -6.36551306e-02\n",
      "  5.83392009e-02 -5.37043177e-02  1.35076880e-01 -1.27263263e-01]\n"
     ]
    }
   ],
   "source": [
    "print(r1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
